<?php
namespace app\index\controller;
use think\Db;
use \Swoole\Http\Response;

class Task
{
    protected $response = null;
    protected $workernum = 5;
    protected $workers = [];
    protected $arr = [];
    protected $path = '/app/tp5/';
    protected $filePath = 'docfile/';
    protected $mfen = 0;
    protected $mi = 0;
    protected $mid = 0;
    protected $mfilename = '';
    protected $mdb = null;
    protected $zipfile = [];
    //从mongo中查询的字段
    protected $mongo_data_fields = array('navigation','abstract_content','content_media_name');
        //从mysql中查询的字段
    protected $mysql_data_fields = array('url','title','media_name','source_type','url_crc','title_crc','id','feature_words','relativity','release_date','add_time');
    function __construct()
    {
        $this->sourceType = array('0'=>'新闻','1'=>'论坛','2'=>'贴吧','3'=>'博客','4'=>'微博','5'=>'问答','6'=>'视频','7'=>'电商','8'=>'网页摘要','9'=>'文库','10'=>'百科','11'=>'微信','12'=>'百度经验','20'=>'新闻评论','30'=>'百度视频','54'=>'客户端','101'=>'其他','104'=>'评论','102'=>'报刊','103'=>'其他','53'=>'外媒');
        $this->relativityType = array('0'=>'中性','1'=>'正面','-1'=>'负面');
        $info = Db::name('export_cache')
                ->where('status','=',1)
                ->limit(5)
                ->select();
        if(!empty($info)){
            $this->arr = $info;
        }
        $count = count($info);
        if(!$count) $this->workernum = 0;
        if($count == 0){
            return '暂无导出任务！';
        }
        if($count != 5){
            $this->workernum = $count;
        }
    }

    public function index(){
        echo "start:".date("Y-m-d H:i:s").PHP_EOL;
        for ($i=0;$i<$this->workernum;$i++){       //开启最多5个子进程执行任务
            $process = new \swoole_process(function (\swoole_process $worker){
                $this->mdb = $db = new DB();
                $this->doExport($worker,$db);
            },true);
            $process->start();
            $process->write($i);
            $this->workers[] = $process;
        }
        //主进程数据结果
        foreach ($this->workers as $process){
            $ret = $process::wait();
        }
        echo "end:".date("Y-m-d H:i:s").PHP_EOL;
    }

    protected function doExport($worker,$db)
    {
        $this->mi = $i = $worker->read();
        trace($this->arr[$i],'arr');
        $db::name('export_cache')->update(['id'=>$this->arr[$i]['id'],'status'=>"2"]);   //先锁定
        $filter = $this->arr[$i]['searchs'];
        $table_lable = "t_lable_group".$this->arr[$i]['table'];
        $table_lableext = "t_lableext_group".$this->arr[$i]['table'];
        $table_repeat = "t_repeat_group".$this->arr[$i]['table'];
        $this->mfilename = $filename = $this->arr[$i]['userid'].'_'.$i.'_'.date('Y-m-d').'_'.time();
        $this->mid =$id = $this->arr[$i]['id'];


        $filter = unserialize($filter);
        $filters = $this->dofilter($filter);
        foreach ($filters as $ks=>$filt){
            $sql_lable = "SELECT ".$this->_filed().",'lab' AS '_from'
                            FROM $table_lable AS lab ".$this->_filter($filt,'lab')." ORDER BY lab.`release_date` DESC;";
            $sql_repeat = "SELECT ".$this->_filed('rep').",'rep' as '_from'
                           FROM $table_repeat AS rep ".$this->_filter($filt,'rep')." ORDER BY rep.`release_date` DESC;";
            //echo $sql_lable;
            //echo $sql_repeat;
            $base_lab = $db::query($sql_lable);//获取lable表中的基础数据$_labs
            //trace(count($base_lab));
            $lable_ids = array();
            foreach($base_lab as $k=>$v){
                $lable_ids[] = $v['id'];
                $temp_arr[$v['title_crc']][] = $v;
            }
            unset($base_lab);
            $base_rep = $db::query($sql_repeat);//获取repeat表中的基础数据$_reps
            //trace(count($base_rep));
            foreach($base_rep as $k=>$v){
                $lable_ids[] = $v['id'];
                $temp_arr[$v['title_crc']][] = $v;
            }
            unset($base_rep);
            if($lable_ids){
                $sql_ext = "SELECT ext.`lable_id`,GROUP_CONCAT(DISTINCT(ext.`key`) SEPARATOR ' ') AS tags
                        FROM `$table_lableext` AS ext WHERE ext.lable_id in ('".implode("','",$lable_ids)."') and ext.keywords_code='".$filt['keywords_code']."'
                        GROUP BY ext.`lable_id`";
                $base_exts = $db::query($sql_ext);//获取ext表中的基础数据$_exts
            }else{
                $base_exts = array();
            }
            foreach ($base_exts as $k=>$v) {
                $all_datas_ext[$v['lable_id']][] = $v['tags'];
            }




            if(count($temp_arr)>=5000){         //超过5000条尝试是否需要分文件存储
                $tmp = $this->dothis($temp_arr,$all_datas_ext);
                $temp_arr = $tmp = $all_datas_ext = [];
                //unset($temp_arr,$tmp,$all_datas_ext);
            }







            unset($lable_ids,$base_exts);
        }
        unset($filters);
        $tmp = $this->dothis($temp_arr,$all_datas_ext);
        $result = $this->download($tmp,$filename.'.xlsx',$id,$db,$i);
        $temp_arr = $tmp = $all_datas_ext = [];
        //unset($temp_arr,$tmp,$all_datas_ext);
        /*$res1 = $this->execCurl($this->url_arr[($i*2)]);
        $res2 = $this->execCurl($this->url_arr[($i*2+1)]);
        echo $res1.PHP_EOL.$res2;*/
    }

    protected function dothis($temp_arr,$all_datas_ext){
        $i = 0;
        $j = 0;
        foreach ($temp_arr as $key=>$value){
            $influence = count($value);
            if($influence==1){
                $tmp[$i]['number'] = $j+1;
                $j++;
                $tmp[$i]['url'] = $value[0]['url'];
                $tmp[$i]['title'] = str_replace(array("\t","\n","\r",","),array("","","","，"),strip_tags($value[0]['title']));
                $tmp[$i]['media_name'] = $value[0]['media_name'];
                //$tmp[] = $v2['content_media_name'];
                //$tmp[] = strip_tags($v2['abstract_content']);
                $tmp[$i]['source_type'] = $this->sourceType[$value[0]['source_type']];
                $tmp[$i]['release_date'] = $value[0]['release_date'];
                $tmp[$i]['feature_words'] = $value[0]['feature_words'];
                $tmp[$i]['influence'] = $influence;
                $tmp[$i]['relativity'] = $this->relativityType[$value[0]['relativity']];
                $tmp[$i]['tags'] = is_array($all_datas_ext[$value[0]['id']])?implode(" ",$all_datas_ext[$value[0]['id']]):"";
            }else{
                foreach ($value as $k=>$item) {
                    if($k == 0){
                        $tmp[$i]['number'] = $j+1;
                        $j++;
                    }else{
                        $tmp[$i]['number'] = '';
                    }
                    $tmp[$i]['url'] = $item['url'];
                    $tmp[$i]['title'] = str_replace(array("\t","\n","\r",","),array("","","","，"),strip_tags($item['title']));
                    $tmp[$i]['media_name'] = $item['media_name'];
                    //$tmp[] = $v2['content_media_name'];
                    //$tmp[] = strip_tags($v2['abstract_content']);
                    $tmp[$i]['source_type'] = $this->sourceType[$item['source_type']];
                    $tmp[$i]['release_date'] = $item['release_date'];
                    $tmp[$i]['feature_words'] = $item['feature_words'];
                    $tmp[$i]['influence'] = $influence;
                    $tmp[$i]['relativity'] = $this->relativityType[$item['relativity']];
                    $tmp[$i]['tags'] = is_array($all_datas_ext[$item['id']])?implode(" ",$all_datas_ext[$item['id']]):"";
                    $i++;
                }
            }
            $i++;

            if(count($tmp)>=10000 && $influence==1){
                $this->mfen += 1;
                $this->doc[$this->mi] = ['isfen'=>1,'level'=>$this->mfen];
                $this->download($tmp,$this->mfilename.'_'.$this->mfen.'.xlsx',$this->mid,$this->mdb,$this->mi,1);
                $tmp = [];
                $i = 0;
                $j = 0;
            }

        }
        return $tmp;
    }

    protected function dofilter($filter)
    {
        //按照时间分割数据
        $pn = strtotime($filter['end_time'])-strtotime($filter['start_time']);
        $pn = ceil($pn/(0.3*24*3600));
        $filters = array();
        if($pn<=1){
            $filters[] = $filter;
        }else{
            $temp = $filter;
            for($i=0;$i<$pn;$i++){
                $temp['start_time'] = date("Y-m-d H:i:s",(strtotime($filter['start_time'])+$i*1*24*3600));
                $temp['end_time'] = date("Y-m-d H:i:s",(strtotime($temp['start_time'])+1*24*3600));
                if($temp['start_time']>$filter['end_time']){
                    break;
                }
                if(strtotime($temp['end_time'])>=strtotime($filter['end_time'])){
                    $temp['end_time'] = $filter['end_time'];
                    $filters[] = $temp;
                    break;
                }else{
                    $filters[] = $temp;
                }
            }
        }
        return $filters;
    }

    protected function _filter($filter,$alias = 'lab'){
        if($filter['similarNum'] == '-99'){
            unset($filter['similarNum']);
        }

        $where[] = 1;
        $where[] = "$alias.`keywords_code` = '".$filter['keywords_code']."'";

        if(isset($filter['url_crc_list']) && is_array($filter['url_crc_list']) && count(array_filter($filter['url_crc_list']))){
            if(isset($filter['url_crc_type']) && $filter['url_crc_type'] == 'notin'){
                $where[] = "$alias.`url_crc` NOT IN ('".implode("','",$filter['url_crc_list'])."')";
            }else{
                $where[] = "$alias.`url_crc` IN ('".implode("','",$filter['url_crc_list'])."')";
            }

        }
        if(isset($filter['title_crc_list']) && is_array($filter['title_crc_list']) && count(array_filter($filter['title_crc_list']))){
            $where[] = "$alias.`title_crc` IN ('".implode("','",$filter['title_crc_list'])."')";
        }
        if(isset($filter['start_time']) && $filter['start_time'] != ''){
            $where[] =  "$alias.`release_date` >= '".$filter['start_time']."'";
            $where[] = "$alias.`release_date_day` >= '".date("Y-m-d",strtotime($filter['start_time']))."'";
        }
        if(isset($filter['end_time']) && $filter['end_time'] != ''){
            $where[] =  "$alias.`release_date` < '".$filter['end_time']."'";
            $where[] =  "$alias.`release_date_day` < '".date("Y-m-d",(strtotime($filter['end_time'])+24*3600) )."'";
        }
        if(isset($filter['add_time_start']) && $filter['add_time_start'] != ''){
            $where[] = "$alias.`add_time` >= '".$filter['add_time_start']."'";
        }
        if(isset($filter['add_time_end']) && $filter['add_time_end'] != ''){
            $where[] = "$alias.`add_time` < '".$filter['add_time_end']."'";
        }
        if(isset($filter['isFeedback']) && $filter['isFeedback'] != ''){
            $where[] = "$alias.`isFeedback` IN (".$filter['isFeedback'].")";
        }
        if(isset($filter['send_status']) && $filter['send_status'] != ''){
            $where[] = "$alias.`send_status` = '".$filter['send_status']."'";
        }
        if(isset($filter['media_name']) && $filter['media_name'] != ''){
            $where[] = "$alias.`media_name` = '".$filter['media_name']."'";
        }

        if(isset($filter['srctype']) && $filter['srctype'] != ''){
            $where[] = "$alias.`source_type` IN (".$filter['srctype'].")";
        }
        if(isset($filter['relativity']) && $filter['relativity'] != ''){
            $where[] = "$alias.`relativity` = ".$filter['relativity'];
        }
        if(isset($filter['isPublish']) && $filter['isPublish'] != ''){
            $where[] = "$alias.`isPublish` = '".$filter['isPublish']."'";
        }

        if(isset($filter['isdeal']) && $filter['isdeal'] != ''){
            $where[] = "$alias.`isdeal` = '".$filter['isdeal']."'";
        }

        if(isset($filter['similarNum']) && $filter['similarNum'] == '-1'){
            $where[] = "$alias.`similarNum` =-1 ";
        }elseif(isset($filter['similarNum']) && $filter['similarNum'] == '-2'){
            $where[] = "$alias.`similarNum` =-2 ";
        }
        if(isset($filter['search']) && $filter['search'] != ''){
            if($filter['search_type'] == 'all'){
                $url = parse_url($filter['search']);
                if ($url['scheme'] == 'https') {
                    $url1 = str_replace("https", "http", $filter['search']);
                } elseif ($url['scheme'] == 'http') {
                    $url1 = str_replace("http", "https", $filter['search']);
                }
                if($url1){
                    $where[] = "($alias.`title` LIKE '%".$filter['search']."%' OR $alias.`feature_words` LIKE '%".$filter['search']."%' OR $alias.`url` LIKE '%".$filter['search']."%' OR $alias.`url` LIKE '%".$url1."%' )";
                }else{
                    $where[] = "($alias.`title` LIKE '%".$filter['search']."%' OR $alias.`feature_words` LIKE '%".$filter['search']."%' OR $alias.`url` LIKE '%".$filter['search']."%' )";
                }

            }
            else{

                if($filter['search_type'] == 'url'){
                    $url = parse_url($filter['search']);
                    if($url['scheme'] == 'https'){
                        $url1 = str_replace("https","http",$filter['search']);
                    }elseif($url['scheme'] == 'http'){
                        $url1 = str_replace("http","https",$filter['search']);
                    }
                    $where[] = "($alias.`".$filter['search_type']."` LIKE '%".$filter['search']."%' OR $alias.`url` LIKE '%".$url1."%')";
                }else{
                    $where[] = "$alias.`".$filter['search_type']."` LIKE '%".$filter['search']."%'";
                }
            }
        }
        return ' WHERE '.implode(' AND ',$where);
    }

    protected function _filed($alias = 'lab',$mysql_fields = array()){
        $sql_field = '';
        $mysql_fields = $mysql_fields ? $mysql_fields : $this->mysql_data_fields;
        foreach($mysql_fields as $field){
            $sql_field .= "$alias.`$field`,";
        }
        return trim($sql_field,',');
    }

    protected function download($arrData=[],$filename='test.csv',$targetid='',$db,$fd,$flag=0)
    {
        ini_set('memory_limit','1024M');
        set_time_limit(0);
        $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
        $sheet       = $spreadsheet->getActiveSheet();
        $sheet->getDefaultRowDimension()->setRowHeight(15);
        $sheet->setCellValue('A1', '序号');
        $sheet->setCellValue('B1', 'url');
        $sheet->setCellValue('C1', '标题');
        $sheet->setCellValue('D1', '来源');
        $sheet->setCellValue('E1', '出处');
        $sheet->setCellValue('F1', '发布时间');
        $sheet->setCellValue('G1', '特征词');
        $sheet->setCellValue('H1', '相似数量');
        $sheet->setCellValue('I1', '正负属性');
        $sheet->setCellValue('J1', '命中标签');
        $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
        $writer->setPreCalculateFormulas(false);
        $i = 1;
        foreach ($arrData as $data) {
            $i++;
            $sheet->setCellValue('A' . $i, $data['number']);
            $sheet->setCellValue('B' . $i, $data['url']);
            $sheet->setCellValue('C' . $i, $data['title']);
            $sheet->setCellValue('D' . $i, $data['media_name']);
            $sheet->setCellValue('E' . $i, $data['source_type']);
            $sheet->setCellValue('F' . $i, $data['release_date']);
            $sheet->setCellValue('G' . $i, $data['feature_words']);
            $sheet->setCellValue('H' . $i, $data['influence']);
            $sheet->setCellValue('I' . $i, $data['relativity']);
            $sheet->setCellValue('J' . $i, $data['tags']);
        }
        unset($arrData);
        $sheet->getColumnDimension('B')->setWidth(30);
        $sheet->getColumnDimension('C')->setWidth(30);
        $sheet->getColumnDimension('F')->setWidth(19);
        if(isset($this->doc[$fd]['isfen']) && $this->doc[$fd]['isfen'] == 1){
            //最后一个分层的名称
            $name = explode('.',$filename);
            if($flag == 0){
                $level = $this->doc[$fd]['level']+1;
                $filename = $name[0].'_'.$level.'.'.$name[1];
                $this->zipfile[] = $this->path.$this->filePath.$filename;
                $writer->save($this->path.$this->filePath.$filename);
                //关闭连接，销毁变量
                $spreadsheet->disconnectWorksheets();
                unset($spreadsheet);
                $this->zip($this->path.$this->filePath.$name[0]);
                $db::name('export_cache')->update(['id'=>$targetid,'file_url'=>"http://127.0.0.1/".$this->filePath.$name[0].'.zip']);
                return 'end';
            }
        }
        $writer->save($this->path.$this->filePath.$filename);
        //关闭连接，销毁变量
        $spreadsheet->disconnectWorksheets();
        unset($spreadsheet);
        if(isset($this->doc[$fd]['isfen']) && $this->doc[$fd]['isfen'] == 1){
            //打包处理
            $this->zipfile[] = $this->path.$this->filePath.$filename;
        }else{
            $db::name('export_cache')->update(['id'=>$targetid,'file_url'=>"http://127.0.0.1/".$this->filePath.$filename]);
        }
    }

    protected function zip($filename){
        trace($this->zipfile,'saob');
        $zipname = $filename.".zip";
        $zip = new \ZipArchive();
        $zip->open($zipname,\ZipArchive::CREATE);   //打开压缩包
        foreach($this->zipfile as $file){
            $res = $zip->addFile($file,basename($file));   //向压缩包中添加文件
        }
        $zip->close();  //关闭压缩包
        foreach ($this->zipfile as $item) {
            //删除源文件
            unlink($item);
        }
        unset($this->zipfile);
        return true;
    }

}